package mesweb.mesweb.controller.procedure;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.Utils.Procedure.MesProcedureSetCircleTableNode;
import mesweb.mesweb.Utils.Procedure.MesProcedureSetCircleTreeNode;
import mesweb.mesweb.Utils.Procedure.MesProcedureSetJsonTree;
import mesweb.mesweb.Utils.Procedure.MesProcedureSetTableTree;
import mesweb.mesweb.entity.Procedure.MesProcedureSet;
import mesweb.mesweb.service.impl.procedure.MesProcedureSetServiceImpl;
import mesweb.mesweb.vo.procecdure.MesProcedureSetVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@Api(tags = "工序组")
@RestController
@RequestMapping("/mes/mesProcedureSet")
public class MesProcedureSetController {
    @Autowired
    private MesProcedureSetServiceImpl mesProcedureSetService;


    @GetMapping("/GetAll")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetAllActionTherbligsTypes(@ApiParam("名称") @RequestParam(value = "name", required = false) String name) {
        List<MesProcedureSet> data = new ArrayList<>();
        if (StringUtils.isBlank(name) || name.equals("全部")) {
            data = mesProcedureSetService.list();
        } else {
            QueryWrapper<MesProcedureSet> queryWrapper = new QueryWrapper<>();
            // queryWrapper.like("Therbligs_Type_Name", name);
            data = mesProcedureSetService.list(queryWrapper);
        }
        String result = "";
        try {
            result = (new MesProcedureSetJsonTree()).Tree();
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
        String newStr = result.replaceAll("\\\\", "");
        MesProcedureSetCircleTreeNode obj = JSON.parseObject(newStr, MesProcedureSetCircleTreeNode.class);
        return CommonResult.success(obj);
    }

    @GetMapping("/GetTreeTableData")
    @ApiOperation("获取全部数据列表")
    public CommonResult<?> GetTreeTableData(@ApiParam("工段ID") @RequestParam(value = "workSectionId", required = false, defaultValue = "0") Long workSectionId, @ApiParam("上级工序组ID") @RequestParam(value = "parentId", required = false, defaultValue = "0") Long parentId) {
        String result;
        try {
            result = (new MesProcedureSetTableTree()).Tree(workSectionId, parentId);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
        String newStr = result.replaceAll("\\\\", "");
        MesProcedureSetCircleTableNode obj = JSON.parseObject(newStr, MesProcedureSetCircleTableNode.class);
        return CommonResult.success(obj);
    }


    /**
     * 分页列表查询
     *
     * @param mesProcedureSet
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "工序组-分页列表查询", notes = "工序组-分页列表查询")
    @GetMapping(value = "/list")
    public CommonResult<?> queryPageList(MesProcedureSetVo mesProcedureSet,
                                         @RequestParam(name = "page", defaultValue = "1") Integer page,
                                         @RequestParam(name = "size", defaultValue = "10") Integer size,
                                         @RequestParam(value = "worksectionId", required = false) Long workSectionId) {
        QueryWrapper<MesProcedureSetVo> queryWrapper = new QueryWrapper<>();
        if (workSectionId != 0L) {
            queryWrapper.eq("Work_Section_Id", workSectionId);
        }
        Page<MesProcedureSetVo> mesProcedureSetPage = new Page<MesProcedureSetVo>(page, size);
        IPage<MesProcedureSetVo> pageList = mesProcedureSetService.getProcedureSet(mesProcedureSetPage, queryWrapper);
        return CommonResult.success(pageList);
    }

    /**
     * 编辑
     *
     * @param mesProcedureSet
     * @return
     */
    @ApiOperation(value = "工序组-编辑", notes = "工序组-编辑")
    @PostMapping(value = "/save")
    public CommonResult<?> edit(@RequestBody MesProcedureSet mesProcedureSet) {

        boolean result = false;
        if (mesProcedureSet.getId() == 0L) {
            QueryWrapper<MesProcedureSet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("procedure_set_no", mesProcedureSet.getProcedureSetNo());
            queryWrapper.or().eq("procedure_set_name", mesProcedureSet.getProcedureSetName());
            MesProcedureSet mesTechnologyFlow1 = mesProcedureSetService.getOne(queryWrapper);
            if (mesTechnologyFlow1 != null) {
                String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getProcedureSetNo(), mesTechnologyFlow1.getProcedureSetName());
                throw new ApiException(msg);
            }
            result = mesProcedureSetService.save(mesProcedureSet);
        } else {
            result = mesProcedureSetService.updateById(mesProcedureSet);
        }
        return CommonResult.success(result);
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "工序组-通过id删除", notes = "工序组-通过id删除")
    @DeleteMapping(value = "/delete")
    public CommonResult<?> delete(@RequestParam(name = "id", required = true) String id) {
        mesProcedureSetService.removeById(id);
        return CommonResult.success("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "工序组-批量删除", notes = "工序组-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public CommonResult<?> deleteBatch(@ApiParam("工序组IDs") @RequestBody List<Long> ids) {
        this.mesProcedureSetService.removeByIds(ids);
        return CommonResult.success("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "工序组-通过id查询", notes = "工序组-通过id查询")
    @GetMapping(value = "/queryById")
    public CommonResult<?> queryById(@RequestParam(name = "id", required = true) String id) {
        MesProcedureSet mesProcedureSet = mesProcedureSetService.getById(id);
        return CommonResult.success(mesProcedureSet);
    }
}
